﻿namespace _14_输入和输出;

internal class Program
{
    private static void Main(string[] args)
    {
        #region 1_流的概念

        // System.IO命名空间包含允许对数据流和文件进行读写的类型 
        // C#为File类中的IO操作提供了高级方法, 为StreamReader或者StreamWriter提供了较为低级的方法
        // 1 处理异常
        // 1_1 IO操作中常见的异常有: FileNotFoundException(文件找不到), DirectoryNotFoundException(目录找不到), PathTooLongException(路径过长), IOException, UnauthorizeAccessException(权限错误, Windows中的文件系统权限)
        // 2 释放资源
        // 2_1 使用using语句可以确保在离开using块时释放资源, 否则需要手动释放
        // 2_2 手动释放的话, 使用Dispose方法, 或者使用IDisposable接口, 在finally子句中手动释放资源

        #endregion

        #region 2_File.ReadAllText: 读取文件的全部内容

        //var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; // 项目基础目录
        //var startupPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); // 启动目录
        //var currentDirectory = Directory.GetCurrentDirectory(); // 当前目录
        //var path = "C:\\Users\\27289\\Desktop\\新建 文本文档.txt";
        ////Console.WriteLine(baseDirectory);
        ////Console.WriteLine(startupPath);
        ////Console.WriteLine(currentDirectory);
        ////Console.ReadKey();
        //var text = File.ReadAllText(path, Encoding.UTF8);

        //Console.WriteLine(text);

        #endregion

        #region 3_File.ReadAllLines: 读取文件的全部内容

        //string[] line = File.ReadAllLines(path, Encoding.UTF8);
        //foreach (var item in line)
        //{
        //    Console.WriteLine(item);
        //}

        #endregion

        #region 4_创建文件, 有则覆盖, 无则创建

        //var dirPath = @"C:\Users\27289\Desktop\txts\";
        //var fileName = "test.txt";
        //if (!Directory.Exists(dirPath))
        //{
        //    Directory.CreateDirectory(dirPath);
        //}
        //using (StreamWriter sw = File.CreateText(dirPath     + fileName))
        //{
        //    sw.WriteLine("Hello World");
        //    sw.WriteLine("Hello World");
        //    sw.WriteLine("BNW");
        //}

        #endregion

        #region 5_获取创建, 更新和最后访问的时间

        //var filePath = @"C:\Users\27289\Desktop\txts\test.txt";
        //if (File.Exists(filePath))
        //{
        //    Console.WriteLine(File.GetCreationTime(filePath));
        //    Console.WriteLine(File.GetLastWriteTime(filePath));
        //    Console.WriteLine(File.GetLastAccessTime(filePath));
        //}

        #endregion

        #region 6_复制文件: File.Copy(string sourceFileName, string destFileName, bool overwrite)

        // var srcPath = @"C:\Users\27289\Desktop\txts\test.txt";
        //var tarPath = @"C:\Users\27289\Desktop\txts\test2.txt";
        // File.Copy(srcPath, tarPath, true);
        // Console.WriteLine("File copied!");

        #endregion

        #region 7_IDisposable接口

        //StreamReader sr = null;
        //var path = @"C:\Users\27289\Desktop\txts\test.txt";

        //try
        //{
        //    sr = new StreamReader(path, Encoding.UTF8);
        //    Console.WriteLine(sr.ReadToEnd());
        //}
        //catch (IOException ex)
        //{
        //    Console.WriteLine("Cannot read files");
        //    Console.WriteLine(ex.Message);
        //}
        //catch (UnauthorizedAccessException ex)
        //{
        //    Console.WriteLine("Cannot access files");
        //    Console.WriteLine(ex.Message);
        //}
        //finally
        //{
        //    sr?.Dispose();
        //}

        #endregion

        #region 8_使用语句

        //// using 语句可以定义一个范围, 确保在离开范围时释放资源(如果出现异常的话, CLR会自动处理其中出现的异常)
        //var path = @"C:\Users\Administrator\Desktop\test123\bak.txt";
        //using (var sr = new StreamReader(path, Encoding.UTF8))
        //{
        //    Console.WriteLine(sr.ReadToEnd());
        //}

        #endregion

        #region 9_using声明

        //// using声明是在using关键字之后的变量声明, 告诉编译器声明的变量应该放在封闭范围的末尾
        //var path = @"C:\Users\Administrator\Desktop\test123\bak.txt";
        //using var sr = new StreamReader(path, Encoding.UTF8);
        //Console.WriteLine(sr.ReadToEnd());

        #endregion

        #region 10_MemoryStream: 处理计算机内存中数据的流

        //using var ms = new MemoryStream();
        //ms.WriteByte(0x41);
        //for (int i = 0; i < 256; i++)
        //{
        //    ms.WriteByte((byte)i);
        //}
        //ms.Position = 0;
        //int rs = ms.ReadByte();

        //do
        //{
        //    Console.WriteLine(rs);
        //    rs = ms.ReadByte();
        //} while (rs != -1);

        #endregion

        #region 11_StreamReader

        //var path = @"C:\Users\27289\Desktop\txts\bak.txt";
        //using var sr = new StreamReader(path, Encoding.UTF8);
        //while (sr.Peek() >= 0) // 返回下一个可用字符, 但是不会移动指针
        //{
        //    Console.WriteLine(sr.ReadLine()); // 没有要读取的字符时, 会返回-1
        //}

        #endregion

        #region 11.5_ReadToEndAsync异步读取文件

        // var path = @"C:\Users\27289\Desktop\txts\bak.txt";
        // using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        // using var sr = new StreamReader(fs, Encoding.UTF8);
        // string content = await sr.ReadToEndAsync();
        // Console.WriteLine(content);

        #endregion

        #region 12_计数行

        //int count = 0;
        //var path = @"C:\Users\Administrator\Desktop\test123\bak.txt";
        //using var sr = new StreamReader(path, Encoding.UTF8);
        //while (sr.ReadLine() != null)
        //{
        //    count++;
        //}
        //Console.WriteLine($"There are {count} lines!");

        #endregion

        #region 13_StreamWriter

        //var path = @"C:\Users\Administrator\Desktop\test123\bak.txt";
        //using (var sw = new StreamWriter(path, true, Encoding.UTF8)) //默认为UTF-8编码
        //{
        //    sw.WriteLine("Hello World");
        //    sw.WriteLine("Today is a windy day!");
        //}
        //using var sr = new StreamReader(path, Encoding.UTF8);
        //string line;
        //while ((line = sr.ReadLine()) != null) 
        //{
        //    Console.WriteLine(line);
        //}

        #endregion

        #region 14_FileStream: 二进制流

        //// FileStream为文件提供流, 同时还支持同步和异步读取和写入操作
        //// StremReader和StreamWriter处理文本数据, 而FileStream处理字节
        //var path = @"C:\Users\Administrator\Desktop\test123\bak.txt";
        //using var fs = new FileStream(path, FileMode.Append);
        //string text = "Фёдор Михайлович Достоевский\n";
        //byte[] bytes = Encoding.UTF8.GetBytes(text);
        //fs.Write(bytes, 0, bytes.Length);
        //fs.Flush(); // 刷新缓冲区
        //Console.WriteLine("Something is to be desired!");

        #endregion

        #region 15_XmlTextReader

        //var path = @"C:\Users\Administrator\Desktop\test123\bak.xml";
        //// XmlTextReader可以用来读取XML文件
        //using (var xReader = new System.Xml.XmlTextReader(path))
        //{
        //    xReader.MoveToContent();
        //    while (xReader.Read())
        //    {
        //        var node = xReader.NodeType switch
        //        {
        //            System.Xml.XmlNodeType.Element => xReader.Name + "\t",
        //            System.Xml.XmlNodeType.Text =>  xReader.Value + "\n",
        //            _ => ""
        //        };
        //        Console.Write(node);
        //    }
        //}

        #endregion

        #region 16_创建和移动目录

        //Directory.CreateDirectory("temp");
        //Directory.CreateDirectory("newDir"); // 创建目录
        //Directory.Move("temp", "tempAnotherName"); // 移动目录,或者说是重命名目录

        #endregion

        #region 17_DirectoyrInfo

        //var path = @"C:\Users\Administrator\Desktop\test123";
        //var dirInfo = new DirectoryInfo(path);
        //string[] files = Directory.GetFiles(path);

        //DirectoryInfo[] dirs = dirInfo.GetDirectories();

        //foreach (var item in dirs) // 没有目录的话, 会返回空数组
        //{
        //    Console.WriteLine(item.Name);
        //}
        //foreach (string fileName in files)
        //{
        //    Console.WriteLine(fileName);
        //}

        #endregion

        #region 18_

        #endregion
    }
}